package com.hello.controller;

import com.hello.dto.UserDTO;
import com.hello.entity.Comments;
import com.hello.entity.Follow;
import com.hello.entity.PLikes;
import com.hello.entity.Post;
import com.hello.result.Result;
import com.hello.service.*;
import com.hello.vo.CommentsVO;
import com.hello.vo.FollowVO;
import com.hello.vo.PLikeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/message")
@Slf4j
@Api(tags = "消息接口")
public class MessageController {
    @Resource
    private IPLikesService plikeService;
    @Resource
    private IFollowService followService;
    @Resource
    private IUserService userService;
    @Resource
    private IPostService postService;
    @Resource
    private ICommentsService commentsService;
    @GetMapping("/getNewLikes/{user_id}")
    @ApiOperation(value = "收到的喜欢")
    public Result getNewLikes(@PathVariable Long user_id){
        List<PLikes> pLikesList = plikeService.getNewPlikes(user_id);
        List<PLikeVO> list = new ArrayList<>();
        for(PLikes pLikes:pLikesList){
            UserDTO userDTO = userService.searchById(pLikes.getLikerId());
            Post post = postService.getPostDetail(user_id,pLikes.getPostOrder());
            PLikeVO pLikeVO = PLikeVO.builder()
                    .userName(userDTO.getUsername())
                    .headshotUrl(userDTO.getHeadshotUrl())
                    .likeTime(pLikes.getLikeTime())
                    .text(post.getText())
                    .imageUrl(post.getImageUrl())
                    .build();
            list.add(pLikeVO);
        }

        return Result.success(list);
    }

    @GetMapping("/getComments/{user_id}")
    @ApiOperation(value = "收到的评论")
    private Result getComments(@PathVariable Long user_id){
        List<Comments> commentsList = commentsService.getComments(user_id);
        List<CommentsVO> list = new ArrayList<>();
        for(Comments comments:commentsList){
            UserDTO userDTO = userService.searchById(comments.getCommenterId());
            Post post = postService.getPostDetail(user_id,comments.getPostOrder());
            CommentsVO commentsVO = CommentsVO.builder()
                    .userName(userDTO.getUsername())
                    .headshotUrl(userDTO.getHeadshotUrl())
                    .commentTime(comments.getCommentTime())
                    .text(comments.getText())
                    .imageUrl(post.getImageUrl())
                    .build();
            list.add(commentsVO);
        }
        return Result.success(list);
    }
//    @GetMapping("/getNewFollowers/{user_id}")
//    @ApiOperation("新的关注")
//    private Result getNewFollowers(@PathVariable Long user_id){
//        List<Follow> followList = followService.getFollower(user_id);
//        Integer newfollowers = 0;
//        for(Follow follow:followList){
//            if(follow.getChecked() == false){
//                newfollowers+=1;
//            }
//        }
//        return Result.success(newfollowers);
//    }
    @GetMapping("/getNewFollowersDetail/{user_id}")
    @ApiOperation(value = "新的关注")
    public Result getNewFollowersDetail(@PathVariable Long user_id){
        List<Follow> followList = followService.getFollower(user_id);
        List<FollowVO> list = new ArrayList<>();
        for(Follow follow:followList) {
            if (follow.getChecked() == false) {
                UserDTO userDTO = userService.searchById(follow.getFollowerId());
                FollowVO followVO = FollowVO.builder()
                        .hostName(userDTO.getUsername())
                        .headshotUrl(userDTO.getHeadshotUrl())
                        .followTime(follow.getFollowTime())
                        .build();
                list.add(followVO);
            }
        }
        return Result.success(list);
    }




}
